package tech.yunland.shop.domain.model;

import com.moyuer.bsf.domain.model.Organ;
import tech.yunland.activity.domain.imodel.BaseModel;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

/**
 * 出入库单
 *
 * @author 王睿智<geekrize@gmail.com>
 */
@Entity
@Table(name = "bill_store")
public class StoreBill extends Bill implements BaseModel {

    private static final long serialVersionUID = -3556531634051178808L;

    @OneToMany(mappedBy = "owner")
    protected Set<StoreBillEntry> entries; // 分录

    @Column(nullable = false)
    private Integer direction = 1; // -1 出库, 1 入库

    @Column(name = "biz_type", nullable = false)
    private Integer biz_type = 0; //-1 普通出库 -2 销售出库  1：普通入库 2：采购入库 3调拨入库 -3调拨出库

    @Column(name = "biz_bill_id")
    private String bizBillId; // 业务单据标识

    @Column(name = "biz_bill_sn")
    private String bizBillSn; //业务单据单号


    @ManyToMany
    @JoinTable(name = "storebill_organ_",
            joinColumns =
            @JoinColumn(name = "storebill_id", referencedColumnName = "id"),
            inverseJoinColumns =
            @JoinColumn(name = "storecontiner_id", referencedColumnName = "id"))
    private List<Organ> storeContiners;


    @ManyToOne
    private Organ storeContiner; //仓库
    @Column(length = 50)
    private String handerName;//经手人

    @Transient
    private String storeContinerId;//仓库ID

    @Transient
    private String storeContinerName;//仓库名称

    @Transient
    private BigDecimal amount;

    @Transient
    private Integer entrySize; //实体数量

    @Transient
    private String entryName; //分录商品名称

    @Transient
    private String bizType;

    @ManyToOne
    private PurchaseBill purchaseBill;


    @ManyToOne
    private SaleBill saleBill;

    private String address; //发货地址

    private String receiver; //收获人名

    @Column(name = "receiver_tel")
    private String receiverTel; //收货人电话

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getReceiverTel() {
        return receiverTel;
    }

    public void setReceiverTel(String receiverTel) {
        this.receiverTel = receiverTel;
    }

    public String getHanderName() {
        return handerName;
    }

    public void setHanderName(String handerName) {
        this.handerName = handerName;
    }

    @Transient
    private String ownerSn;

    public BigDecimal getAmount() {
        BigDecimal total = BigDecimal.ZERO;
        for (StoreBillEntry entry : entries) {
            if (entry.getState() != null && entry.getState().intValue() == 1) {
                total = total.add(entry.getAmount());
            }
        }
        return total;
    }

    public Organ getStoreContiner() {
        return storeContiner;
    }

    public void setStoreContiner(Organ storeContiner) {

        this.storeContiner = storeContiner;
    }

    public String getStoreContinerId() {
        if (storeContiner != null) {
            this.storeContinerId = storeContiner.getId();
        }
        return this.storeContinerId;
    }

    public void setStoreContinerId(String storeContinerId) {
        this.storeContinerId = storeContinerId;
    }

    public String getStoreContinerName() {
        if (storeContiner != null) {
            this.storeContinerName = storeContiner.getName();
        }
        return this.storeContinerName;
    }

    public void setStoreContinerName(String storeContinerName) {
        this.storeContinerName = storeContinerName;
    }

    public List<Organ> getStoreContiners() {
        return storeContiners;
    }

    public void setStoreContiners(List<Organ> storeContiners) {
        this.storeContiners = storeContiners;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public Set<StoreBillEntry> getEntries() {
        return entries;
    }

    public void setEntries(Set<StoreBillEntry> entries) {
        this.entries = entries;
    }

    public Integer getDirection() {
        return direction;
    }

    public void setDirection(Integer direction) {
        this.direction = direction;
    }

    public Integer getBiz_type() {
        return biz_type;
    }

    public void setBiz_type(Integer biz_type) {
        this.biz_type = biz_type;
    }

    public String getBizBillId() {
        return bizBillId;
    }

    public void setBizBillId(String bizBillId) {
        this.bizBillId = bizBillId;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    public Integer getEntrySize() {
        int i = 0;
        for (StoreBillEntry entry : entries) {
            if (entry.getState() != null && entry.getState().intValue() != -1) {
                i++;
            }
        }
        return Integer.valueOf(i);
    }

    public void setEntrySize(Integer entrySize) {
        this.entrySize = entrySize;
    }

    public String getEntryName() {
        String names = "";
        for (StoreBillEntry entry : entries) {
            if (entry.getState() != null && entry.getState().intValue() != -1) {
                names += entry.getGoodsName() + ",";
            }
        }
        return names.length() > 1 ? names.substring(0, names.length() - 1) : "";
    }

    public void setEntryName(String entryName) {
        this.entryName = entryName;
    }

    @Column(name = "recently_approval")
    private String recentlyApproval;

    private String result;

    @Override
    public void setRecentlyApproval(String s) {
        this.recentlyApproval = s;
    }

    @Override
    public void setResult(String s) {
        this.result = s;
    }

    public String getRecentlyApproval() {
        return recentlyApproval;
    }

    public String getResult() {
        return result;
    }

    @Column(name = "processdef_id")
    private String processdefId;//流程id

    public String getProcessdefId() {
        return processdefId;
    }

    public void setProcessdefId(String processdefId) {
        this.processdefId = processdefId;
    }

    public String getBizType() {
//        -1 普通出库 -2 销售出库  1：普通入库 2：采购入库
        switch (biz_type.intValue()) {
            case -1:
                bizType = "普通出库";
                break;
            case -2:
                bizType = "销售出库";
                break;
            case 1:
                bizType = "普通入库";
                break;
            case 2:
                bizType = "采购入库";
                break;
            case 3:
                bizType = "调拨入库";
                break;
            case -3:
                bizType = "调拨出库";
                break;
        }
        return bizType;
    }

    public void setBizType(String bizType) {
        this.bizType = bizType;
    }

    public PurchaseBill getPurchaseBill() {
        return purchaseBill;
    }

    public void setPurchaseBill(PurchaseBill purchaseBill) {
        this.purchaseBill = purchaseBill;
    }

    public SaleBill getSaleBill() {
        return saleBill;
    }

    public void setSaleBill(SaleBill saleBill) {
        this.saleBill = saleBill;
    }

//    public String getOwnerSn() {
//
//        if (this.purchaseBill != null) {
//            return this.purchaseBill.getSn();
//        } else if (this.saleBill != null) {
//            return this.saleBill.getSn();
//        }
//        else if (this.allotBill != null) {
//            return this.allotBill.getSn();
//        }else {
//            return "无";
//        }
//    }

    public void setOwnerSn(String ownerSn) {
        this.ownerSn = ownerSn;
    }

    public String getBizBillSn() {
        return bizBillSn;
    }

    public void setBizBillSn(String bizBillSn) {
        this.bizBillSn = bizBillSn;
    }

//    public AllotBill getAllotBill() {
//        return allotBill;
//    }
//
//    public void setAllotBill(AllotBill allotBill) {
//        this.allotBill = allotBill;
//    }
}
